package com.ruoyi.dynamic.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dynamic.domain.UserDynamicAuto;
import com.ruoyi.dynamic.service.IUserDynamicAutoService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.date.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisUtil;

/**
 * 动态自动发布设置Controller
 * 
 * @author DEREK
 * @date 2024-08-12
 */
@RestController
@RequestMapping("/dynamic/dynamicAuto")
public class UserDynamicAutoController extends BaseController
{
    @Autowired
    private IUserDynamicAutoService userDynamicAutoService;

    /**
     * 查询动态自动发布设置列表
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamicAuto:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserDynamicAuto userDynamicAuto)
    {
        startPage();
        List<UserDynamicAuto> list = userDynamicAutoService.selectUserDynamicAutoList(userDynamicAuto);
        return getDataTable(list);
    }

    /**
     * 导出动态自动发布设置列表
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamicAuto:export')")
    @Log(title = "动态自动发布设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserDynamicAuto userDynamicAuto)
    {
        List<UserDynamicAuto> list = userDynamicAutoService.selectUserDynamicAutoList(userDynamicAuto);
        ExcelUtil<UserDynamicAuto> util = new ExcelUtil<UserDynamicAuto>(UserDynamicAuto.class);
        util.exportExcel(response, list, "动态自动发布设置数据");
    }

    /**
     * 获取动态自动发布设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamicAuto:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userDynamicAutoService.getById(id));
    }

    /**
     * 新增动态自动发布设置
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamicAuto:add')")
    @Log(title = "动态自动发布设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserDynamicAuto userDynamicAuto)
    {
        return toAjax(userDynamicAutoService.save(userDynamicAuto));
    }
    
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 修改动态自动发布设置
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamicAuto:edit')")
    @Log(title = "动态自动发布设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserDynamicAuto userDynamicAuto)
    {
    	if(null != userDynamicAuto.getTime()) {
    		
    		if(1 == userDynamicAuto.getType()) {
    			String timeKey = "dynamic_auto_time";
            	redisUtil.set(timeKey, DateUtils.dateToString(userDynamicAuto.getTime(), "yyyy-MM-dd HH:mm:ss"));
    		}else {
    			String timeKey = "party_auto_time";
    	    	redisUtil.set(timeKey, DateUtils.dateToString(userDynamicAuto.getTime(), "yyyy-MM-dd HH:mm:ss"));
    		}
    	}
    	if(StringUtils.isNotEmpty(userDynamicAuto.getObjId())) {
    		if(1 == userDynamicAuto.getType()) {
    			String key = "dynamic_auto0812";
    	    	redisUtil.set(key, userDynamicAuto.getObjId());
    		}else {
    			String key = "party_auto0812";
    	    	redisUtil.set(key, userDynamicAuto.getObjId());
    		}
    	}else {
    		if(1 == userDynamicAuto.getType()) {
    			String key = "dynamic_auto0812";
    			redisUtil.del(key);
    		}else {
    			String key = "party_auto0812";
    	    	redisUtil.del(key);
    		}
    	}
        return toAjax(userDynamicAutoService.updateById(userDynamicAuto));
    }

    /**
     * 删除动态自动发布设置
     */
    @PreAuthorize("@ss.hasPermi('dynamic:dynamicAuto:remove')")
    @Log(title = "动态自动发布设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userDynamicAutoService.deleteUserDynamicAutoByIds(ids));
    }
}
